package com.example.hello;

import android.app.AlertDialog;
import android.content.Context;
import android.content.DialogInterface;
import android.content.Intent;
import android.content.SharedPreferences;
import android.os.Bundle;
import android.os.Handler;
import android.view.View;
import android.widget.Button;
import android.widget.CheckBox;
import android.widget.EditText;
import android.widget.LinearLayout;

import androidx.appcompat.app.AppCompatActivity;


public class MainActivity extends AppCompatActivity {

    private Button btn;
    private EditText name;
    private EditText pwd;
    private Button btn1;
    private LinearLayout linear;
    private Button btn2;
    private CheckBox check;
    private CheckBox autologin;
    private String ifcheck;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
        initUI();
        //读取数据
        SharedPreferences sp1 = getSharedPreferences("data", Context.MODE_PRIVATE);
        String name1 = sp1.getString("name", null);
        String pwd1 = sp1.getString("pwd", null);
        String auto = sp1.getString("auto", "");
        name.setText(name1);
        pwd.setText(pwd1);

        //自动登录
        if (name1 != null && pwd1 != null) {
            autologin.setChecked(auto.equals("true"));
            if (autologin.isChecked()) {
                new Handler().postDelayed(new Runnable() {
                    @Override
                    public void run() {
                        if (autologin.isChecked()) {
                            Intent intent = new Intent(MainActivity.this, LoginActivity.class);
                            startActivity(intent);
                            MainActivity.this.finish();
                        }
                    }
                }, 1000 * 3);
            }
        }

        //登录功能
        btn.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {

                if (name.getText().toString().equals("admin") && pwd.getText().toString().equals("123")
                        || name.getText().toString().equals("user") && pwd.getText().toString().equals("123")
                ) {
                    Intent intent = new Intent(MainActivity.this, LoginActivity.class);
                    startActivity(intent);
                    SharedPreferences sp = getSharedPreferences("data", Context.MODE_PRIVATE);
                    SharedPreferences.Editor edit = sp.edit();
                    if (autologin.isChecked()) {
                        ifcheck = "true";
                        edit.putString("auto", ifcheck);
                    }
                    if (check.isChecked()) {
                        edit.putString("pwd", pwd.getText().toString().trim());
                    }
                    edit.putString("name", name.getText().toString().trim());
                    edit.apply();
                } else {
                    showDialog();
                }
            }
        });

        //切换布局功能
        btn1.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                if (linear.getOrientation() == LinearLayout.HORIZONTAL) {
                    linear.setOrientation(LinearLayout.VERTICAL);
                } else {
                    linear.setOrientation(LinearLayout.HORIZONTAL);
                }
            }
        });

        //清除
        btn2.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                name.setText(null);
                pwd.setText(null);
                autologin.setChecked(false);
                SharedPreferences sp = getSharedPreferences("data", Context.MODE_PRIVATE);
                SharedPreferences.Editor edit = sp.edit();
                edit.clear();
                edit.apply();
            }
        });


    }

    //绑定事件
    public void initUI() {
        btn = findViewById(R.id.bt_call);
        name = findViewById(R.id.UserName);
        pwd = findViewById(R.id.PWD);
        btn1 = findViewById(R.id.bt_change);
        linear = findViewById(R.id.Linear);
        btn2 = findViewById(R.id.clean);
        check = findViewById(R.id.checkBox);
        autologin = findViewById(R.id.autologin);
    }


    private void showDialog() {
        AlertDialog.Builder builder = new AlertDialog.Builder(this);
        builder.setTitle("提示");
        builder.setMessage("账户名或密码错误");
        builder.setPositiveButton("关闭",
                new DialogInterface.OnClickListener() {
                    @Override
                    public void onClick(DialogInterface dialogInterface, int i) {
                    }
                });
        AlertDialog dialog = builder.create();
        dialog.show();
    }
}